package com.qf.java2107.yb.hotel.controller.food;

import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.constant.MethodConstant;
import com.qf.java2107.yb.hotel.controller.BaseServlet;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.Food;
import com.qf.java2107.yb.hotel.service.FoodService;
import com.qf.java2107.yb.hotel.service.FoodTypeService;
import com.qf.java2107.yb.hotel.utils.UploadFileUtils;

import javax.servlet.ServletContext;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;

/**
 * @author yb
 * @version 1.0
 * @date 2021-10-26
 **/
@WebServlet("/food")
@MultipartConfig
public class FoodController extends BaseServlet {

    private FoodService foodService = (FoodService) BeanFactory.getBean("foodService");

    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("foodTypeService");

    /**
     * 跳转增加页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String saveui(HttpServletRequest request, HttpServletResponse response) throws Exception{

        //获取菜系列表
        ResultVO vo = foodTypeService.findAll();

        //放到request中
        if(vo.getSuccess()) {
            request.setAttribute("types", vo.getData());
            //转发
            return MethodConstant.FORWARD_PREFIX + getServletContext().getAttribute("backendDetailFolder") + "/food/food-save.jsp";
        }
        return (String) vo.getData();

    }

    /**
     * 增加保存页面
     * 文件上传方式，只需要添加，然后保存
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception{

        //上传文件
        //result 就是文件存储的地址
        String result = UploadFileUtils.upload(request);

        if("0".equals(result)) {
            return "<script>alert('菜品图片必须上传，不能为空');location.href='" + request.getContextPath() + "/food?method=saveui';</script>";
        } else if("-1".equals(result)) {
            return "<script>alert('菜品图片上传失败，请联系管理员');location.href='" + request.getContextPath() + "/food?method=saveui';</script>";
        }
        //前端获取参数
        String foodName = request.getParameter("foodName");
        String foodPrice = request.getParameter("price");
        String foodMprice = request.getParameter("mprice");
        String foodDesc = request.getParameter("introduce");
        String typeId = request.getParameter("typeId");
        String imageUrl = UploadFileUtils.upload(request);

        Food food = new Food();
        food.setFoodName(foodName);
        food.setFoodPrice(new BigDecimal(foodPrice));
        food.setFoodDesc(foodDesc);
        food.setFoodImage(imageUrl);
        food.setFoodMprice(new BigDecimal(foodMprice));
        food.setTypeId(Integer.parseInt(typeId));
        food.setFoodImage(result);
        foodService.foodAdd(food);

        return MethodConstant.REDIRECT_PREFIX + "/food?method=list";    //查询所有菜品数据的Servlet";

    }

    //跳转更改页面
    public String updateui(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //查询菜系列表
        ResultVO vo = foodTypeService.findAll();
        ServletContext servletContext = request.getServletContext();
        servletContext.setAttribute("types",vo.getData());

        //查询菜品数据
        String foodId = request.getParameter("foodId");
        ResultVO resultVO = foodService.finById(Integer.parseInt(foodId));
        if(resultVO.getSuccess()){
            servletContext.setAttribute("food",resultVO.getData());

            //把回显的图片放进Context域对象中
            Food food = (Food) resultVO.getData();
            getServletContext().setAttribute("image",food.getFoodImage());
            return MethodConstant.FORWARD_PREFIX+getServletContext().getAttribute("backendDetailFolder")+"/food/food-update.jsp";
        }
        return resultVO.getMessage();
    }


    public String list(HttpServletRequest request, HttpServletResponse response){

        ResultVO vo = foodService.list();

        if (vo.getSuccess()){
            request.setAttribute("types", vo.getData());
            //转发
            return MethodConstant.FORWARD_PREFIX + getServletContext().getAttribute("backendDetailFolder") + "/food/food-list.jsp";
        }
        return (String) vo.getData();
    }


    public String delete(HttpServletRequest request, HttpServletResponse response){

        String foodId = request.getParameter("foodId");
        ResultVO vo = foodService.delete(Integer.parseInt(foodId));
        if (vo.getSuccess()){
            request.setAttribute("deleteId", vo.getData());
            //转发
            return MethodConstant.FORWARD_PREFIX + getServletContext().getAttribute("ctx") + "/food?method=list";
        }
        return (String) vo.getData();

    }

    public String inserch(HttpServletRequest request, HttpServletResponse response){

        String foodName = request.getParameter("foodName");
        ResultVO vo = foodService.findLikeFoodName(foodName);
        if (vo.getSuccess()){
            request.setAttribute("foodName", vo.getData());
            //转发
            return MethodConstant.FORWARD_PREFIX + getServletContext().getAttribute("ctx") + "/food?method=list";
        }
        return (String) vo.getData();
    }



    public String update(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        String result = UploadFileUtils.upload(request);
        if("0".equals(result)){
            result =(String)getServletContext().getAttribute("image");
        }else if("-1".equals(result)){
            return "<script>alert('菜品图片文件上传失败');location.href='"+request.getContextPath()+"/food?method=updateui';</script>";
        }

        String typeId = request.getParameter("typeId");
        String foodId = request.getParameter("foodId");
        String foodName = request.getParameter("foodName");
        String foodPrice = request.getParameter("foodPrice");
        String foodMprice = request.getParameter("foodMprice");
        String foodDesc = request.getParameter("foodDesc");

        //封装进实体里
        Food food = new Food();
        food.setFoodId(Integer.valueOf(foodId));
        food.setTypeId(Integer.valueOf(typeId));
        food.setFoodName(foodName);
        food.setFoodPrice(new BigDecimal(foodPrice));
        food.setFoodMprice(new BigDecimal(foodMprice));
        food.setFoodImage(result);
        food.setFoodDesc(foodDesc);

        ResultVO resultVO = foodService.update(food);
        if(resultVO.getSuccess()){
            return MethodConstant.REDIRECT_PREFIX+request.getContextPath()+"/food?method=list";
        }
        return resultVO.getMessage();
    }
}
